//
//  VisitorView.swift
//  weibo
//
//  Created by Fly on 2017/12/28.
//  Copyright © 2017年 Fly. All rights reserved.
//

import UIKit

/// 访客视图
class VisitorView: UIView {

    // 访客视图信息字典
    var visitorInfo: [String: String]? {
    
        didSet {
        
            guard let imageName = visitorInfo?["imageName"], let message = visitorInfo? [ "message"] else {
                
                return
                
            }
            
            centerLabel.text = message
            
            if imageName == "" {// 如果是首页mainImageView则显示默认图片
                
                startAnimation()
                
                return
                
            }
            
            mainImageView.image = UIImage(named: imageName)
            maskImageView.isHidden = true
            bgImageView.isHidden = true
            
//            startAnimation()
            
        }
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 私有控件
    fileprivate lazy var mainImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "bigHome"))
    
    fileprivate lazy var maskImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "mask"))
    
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "bgCircle"))
    
    fileprivate lazy var centerLabel: UILabel = UILabel.easyLabel(textString: "关注Swift的测试demo,关注Swift的测试demo,关注Swift的测试demo,关注Swift的测试demo,关注Swift的测试demo", fontSize: 14.0, textAlignment: .center, textStrColor: UIColor.darkGray)
    
    lazy var registerBtn: UIButton = UIButton(title: "注册", titleNormalColor: UIColor.orange, titleHighlightedColor: UIColor.black)
    
    lazy var loginBtn: UIButton = UIButton.easyBtn(title: "登录", titleNormalColor: UIColor.blue, titleHighlightedColor: UIColor.black)
    
}

// MARK: 设置界面
extension VisitorView {
    
    fileprivate func setupUI() {
        
        backgroundColor = UIColor.white
        
        // 原生自动布局: 构造函数、自动布局类函数
        // 1.先添加控件
        addSubview(bgImageView)
        addSubview(maskImageView)
        addSubview(mainImageView)
        addSubview(centerLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 2.取消autoresizing，否则autolayout不生效
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
            
        }
        
        // 3.自动布局 构造函数
        
        let margin: CGFloat = 20.0
        
        // 1>.转动背景
        let bgImageViewX = NSLayoutConstraint(
            item: bgImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0.0)
        
        let bgImageViewY = NSLayoutConstraint(
            item: bgImageView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerY,
            multiplier: 1.0,
            constant: -2 * margin)
        
        //  原生自动布局约束宽高的两种方式
//        let bgImageViewHeight = NSLayoutConstraint(
//            item: bgImageView,
//            attribute: .height,
//            relatedBy: .equal,
//            toItem: self,
//            attribute: .width,
//            multiplier: 0.5,
//            constant: 0.0)
        
        let bgImageViewHeight = NSLayoutConstraint(
            item: bgImageView,
            attribute: .height,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 11 * margin)
        
        let bgImageViewWidth = NSLayoutConstraint(
            item: bgImageView,
            attribute: .width,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .height,
            multiplier: 1.0,
            constant: 0.0)
        
        let bgImageSizeArray: Array = [bgImageViewX, bgImageViewY, bgImageViewWidth,  bgImageViewHeight]
        
        addConstraints(bgImageSizeArray)
        
        // 2>.中心图片
        let mainImageViewX = NSLayoutConstraint(
            item: mainImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0.0)
        
        let mainImageViewY = NSLayoutConstraint(
            item: mainImageView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0.0)
        
        let mainImageViewWidth = NSLayoutConstraint(
            item: mainImageView,
            attribute: .width,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .height,
            multiplier: 0.5,
            constant: 0.0)
        
        let mainImageViewHeight = NSLayoutConstraint(
            item: mainImageView,
            attribute: .height,
            relatedBy: .equal,
            toItem: mainImageView,
            attribute: .width,
            multiplier: 1.0,
            constant: 0.0)
        
        let mainImageSizeArray: Array = [mainImageViewX, mainImageViewY, mainImageViewWidth,  mainImageViewHeight]
        
        addConstraints(mainImageSizeArray)
        
        // 3>.说明文本
        addConstraint(NSLayoutConstraint(
            item: centerLabel,
            attribute: .topMargin,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .bottom,
            multiplier: 1.0,
            constant: margin))
        
        addConstraint(NSLayoutConstraint(
            item: centerLabel,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0.0))
        
        // 约束Label左右距离屏幕左右的边距
        addConstraint(NSLayoutConstraint(
            item: centerLabel,
            attribute: .leftMargin,
            relatedBy: .equal,
            toItem: self,
            attribute: .left,
            multiplier: 1,
            constant: 3 * margin))
        
        addConstraint(NSLayoutConstraint(
            item: centerLabel,
            attribute: .rightMargin,
            relatedBy: .equal,
            toItem: centerLabel,
            attribute: .leftMargin,
            multiplier: 1,
            constant: 0.0))
        
        // 4>.注册按钮
        addConstraint(NSLayoutConstraint(
            item: registerBtn,
            attribute: .topMargin,
            relatedBy: .equal,
            toItem: centerLabel,
            attribute: .bottom,
            multiplier: 1.0,
            constant: margin))
        
        addConstraint(NSLayoutConstraint(
            item: registerBtn,
            attribute: .left,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .left,
            multiplier: 1.0,
            constant: 0.0))
        
        // 5>.登录按钮
        addConstraint(NSLayoutConstraint(
            item: loginBtn,
            attribute: .top,
            relatedBy: .equal,
            toItem: registerBtn,
            attribute: .top,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: loginBtn,
            attribute: .right,
            relatedBy: .equal,
            toItem: bgImageView,
            attribute: .right,
            multiplier: 1.0,
            constant: 0.0))
        
        // 6>.mask图像 (自动布局 VFL)
        let viewsDic: [String: Any] = ["bgImageView":bgImageView, "centerLabel":centerLabel, "maskImageView": maskImageView]
        
        let metrics = ["space": margin * 0.5, "top":-bgImageView.bounds.height * 0.35];
        
        // metrics: 定义VFL中（）指定的常数映射关系 
        // views: 定义VFL中控件名称和实际名称[]的映射关系
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskImageView]-0-|",
            options: [],
            metrics: nil,
            views: viewsDic))
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:[bgImageView]-(top)-[maskImageView]-(space)-[centerLabel]",
            options: [],
            metrics: metrics,
            views: viewsDic))
        
    }

    // 旋转动画
    fileprivate func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        // 动画执行一次所需的时间 s
        anim.duration = 15
        
        // 动画完成后不删除，如果为true，添加动画的图层被释放，动画会一起销毁
        anim.isRemovedOnCompletion = false
        
        bgImageView.layer.add(anim, forKey: nil)
        
    }



}
